package security.db;

import java.util.List;
import java.util.Map;
import java.util.Vector;


public class DbQuery {
	private String name;
	private String queryTemplate;
	private String queryType;
	
	// SLQ Operations
	public static final String SELECT_QUERY="SELECT";
	public static final String UPDATE_QUERY="UPDATE";
	public static final String INSERT_QUERY="INSERT";
	public static final String DELETE_QUERY="DELETE";
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getQueryTemplate() {
		return queryTemplate;
	}
	public void setQueryTemplate(String queryTemplate) {
		this.queryTemplate = queryTemplate;		
	}
	public String getQueryType() {
		return queryType;
	}
	public void setQueryType(String queryType) {
		this.queryType = queryType;
	}
	
	public String constructQuery(Map<String,Object> params) {
		String sqlQuery = null;
		String real;
		String type;
		
		if(queryTemplate != null){
			sqlQuery = "";
			int startPos = 0;
			int endPos = 0;
			int searchingPos = 0;
			int appendingPos = 0;
			int TEMPLATE_SIZE = queryTemplate.length();
						
			while((searchingPos<TEMPLATE_SIZE) && (startPos != -1)){
				startPos = queryTemplate.indexOf(":",searchingPos);
				if(startPos != -1){
					sqlQuery += queryTemplate.substring(appendingPos,startPos);
					searchingPos = startPos + 1;
					endPos = queryTemplate.indexOf(":",searchingPos);
					if(endPos == -1){
						// Error here
						System.out.println("Error for handling query template");
						sqlQuery = null;
						startPos = -1;
					} else {
						String param_name = queryTemplate.substring(startPos+1,endPos);
						//ex: param_name = userID$int
						
						int place = param_name.indexOf('$');
						
						real = param_name.substring(0, place); //ex: real = userID
						type = param_name.substring(place + 1);// ex: type = int
						
						if(params.containsKey(real)){
							if(type.equals("string"))
							{
								String str = (String)params.get(real);
								str = str.replace("'","''");
								sqlQuery += " '" + str + "' ";
							} else if(type.equals("int")){
								Integer i = (Integer)params.get(real);
								sqlQuery += " " + i.intValue() + " ";
							} else if(type.equals("double")) {
								Double d = (Double)params.get(real);
								sqlQuery += " " + d.doubleValue() + " ";
							}
								
							appendingPos = endPos + 1;
							searchingPos = endPos + 1;
						} else {
							// Error here
							System.out.println("Error when handling query template - no corresponding parameters");
							sqlQuery = null;
							startPos = -1;
						}
						
					}
				} else {
					// No parameter here
					sqlQuery += queryTemplate.substring(appendingPos,TEMPLATE_SIZE);
				}	
				
			}
			
		}
		return sqlQuery;
	}
	
	public List<DbQueryParam> getQueryParams() {
		List<DbQueryParam> queryParams = null;
		if(queryTemplate != null) {			
			String real;
			String type;
			int startPos = 0;
			int endPos = 0;
			int searchingPos = 0;			
			int TEMPLATE_SIZE = queryTemplate.length();
			queryParams = new Vector<DbQueryParam>(2);
			
			while((searchingPos<TEMPLATE_SIZE) && (startPos != -1)) {
				startPos = queryTemplate.indexOf(":",searchingPos);
				if(startPos != -1){					
					searchingPos = startPos + 1;
					endPos = queryTemplate.indexOf(":",searchingPos);
					if(endPos == -1){
						// Error here
						System.out.println("Error for handling query template");						
						startPos = -1;
					} else {
						String param_name = queryTemplate.substring(startPos+1,endPos);
						//ex: param_name = userID$int
							
						int place = param_name.indexOf('$');
							
						real = param_name.substring(0, place); //ex: real = userID
						type = param_name.substring(place + 1);// ex: type = int

						if(type.equals("string"))
						{
							type = "java.lang.String";
						} 	else if(type.equals("int")){
							type = "int";
						} else if(type.equals("double")) {
							type = "double";
						} else if(type.equals("date")) {							
							type = "java.util.Date";
						} else if(type.equals("datetime")) {
							type = "java.util.Date";
						} else if(type.equals("boolean")) {
							type = "boolean";
						}
						
						// Constructing DbQueryParam
						DbQueryParam param = new DbQueryParam(real, type);
						queryParams.add(param);
						searchingPos = endPos + 1;
					}
						
				}
			}	
		}		
		return queryParams;
	}	
	
	
}
